// pages/circle/evaluatelist.js
const app = new getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    replylist:[],
    iscontent:true,
    evaluateid:0,
    evaluatelist:[],
    isShow:true,
    isTq:false,
    is_no:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //console.log(options);
    this.setData({
      evaluateid: options.id
    })
    //获取单条评论内容
    this.getEvaluateone(options.id);
    //获取回复列表
    this.getReplylist(options.id);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //获取单条评论内容
  getEvaluateone:function(options){
    var that = this;
    wx.request({
      url: app.globalData.serverUrl + 'evaluateone',
      data: {
        id: options
      },
      success: function (res) {
        //console.log(res);
        that.setData({
          evaluatelist: res.data
        })
      }
    })
  },
  //获取回复列表
  getReplylist:function(options){
    var that = this;
    var userid = app.globalData.openid;
    wx.request({
      url: app.globalData.serverUrl + 'replylist',
      data: {
        id:options,
        userid:userid
      },
      success: function(res) {
        console.log(res);
        if (res.data.status == 0){
          that.setData({
            replylist:[],
            is_no:false
          })
        } else{
          that.setData({
            replylist: res.data.replylist,
            is_no:true
          })
        }
      }
    })
  },
  //评论内容
  input: function (e) {
    if (e.detail.value) {
      this.setData({
        content: e.detail.value,
        iscontent: false
      })
    } else {
      this.setData({
        iscontent: true
      })
    }

  },
  //发布
  send: function () {
    var that = this;
    var content = that.data.content;
    var evaluateid = that.data.evaluateid;
    var userid = app.globalData.openid;
    wx.request({
      url: app.globalData.serverUrl + 'replyadd',
      data: {
        userid: userid,
        evaluateid: evaluateid,
        content: content
      },
      success: function (res) {
        //console.log(res);
        if (res.data.status == 1) {
          wx.showToast({
            title: '发布成功',
            icon: 'success',
            duration: 2000,
            success: function (e) {
              //console.log(circleid);
              that.getReplylist(evaluateid)
            }
          })
        } else if(res.data.status == 2){
          wx.showToast({
            title: '请先登录',
            icon: 'none',
            duration: 2000
          })
        }else{
          wx.showToast({
            title: '发布失败，请重新尝试',
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  //评论内容点赞判断
  onZan:function(){
    var that = this;
    var evaluateid = that.data.evaluateid;
    var userid = app.globalData.openid;
    wx.request({
      url: app.globalData.serverUrl + 'zanadd',
      data: {
        evaluateid: evaluateid,
        userid: userid
      },
      success: function(res) {
        if (res.data) {
          that.setData({
            fabulous_num: res.data.num,
            is_gz: res.data.is_gz
          }),
            wx.showToast({
              title: 'ok~',
              icon: 'success',
              mask: true,
              success: function (e) {
                that.getEvaluateone(evaluateid);
              }
            })
        }
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  //回复内容点赞判断
  onFabulous: function (e) {
    var that = this;
    var postId = e.currentTarget.dataset.postid;
    var evaluateid = that.data.evaluateid;
    var userid = app.globalData.openid;
    wx.request({
      url: app.globalData.serverUrl + 'fabulousadd',
      data: {
        id: postId,
        userid:userid
      },
      success: function (res) {
        if (res.data) {
          that.setData({
            fabulous_num: res.data.num,
            is_gz: res.data.is_gz
          }),
            wx.showToast({
              title: 'ok~',
              icon: 'success',
              mask: true,
              success: function (e) {
                that.getReplylist(evaluateid);
              }
            })
        }
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  //删除回复内容
  delreply:function(e){
    var that = this;
    var replyid = e.currentTarget.dataset.postid;
    var evaluateid = that.data.evaluateid;
    wx.request({
      url: app.globalData.serverUrl + 'delreply',
      data: {
        replyid: replyid
      },
      success: function (res) {
        if (res.data.status == 1) {
          wx.showToast({
            title: '删除成功',
            icon: 'success',
            duration: 2000,
            mask:true
          })
          that.getReplylist(evaluateid);
        } else {
          wx.showToast({
            title: '删除失败',
            icon: 'none',
            duration: 2000,
            mask: true
          })
        }
      }
    })
  },
  //回复已经回复的内容
  replyadd_reply:function(){
    var that = this;
    var userid = app.globalData.openid;
    var replyid = that.data.replyid;
    var evaluateid = that.data.evaluateid;
    var content = that.data.content;
    wx.request({
      url: app.globalData.serverUrl +'replyadd_reply',
      data: {
        userid:userid,
        replyid: replyid,
        evaluateid: evaluateid,
        content:content
      },
      success: function(res) {
        if (res.data.status == 1) {
          wx.showToast({
            title: '发布成功',
            icon: 'success',
            duration: 2000,
            mask: true,
            success: function (e) {
              //console.log(circleid);
              that.getReplylist(evaluateid)
            }
          })
        } else if (res.data.status == 2) {
          wx.showToast({
            title: '请先登录',
            icon: 'none',
            duration: 2000,
            mask: true
          })
        } else {
          wx.showToast({
            title: '发布失败，请重新尝试',
            icon: 'none',
            duration: 2000,
            mask: true
          })
        }
      }
    })
  },
  // 点击回复
  clickReply:function(e){
    var replyid = e.currentTarget.dataset.postid;
    this.setData({
      isShow:false,
      isTq:true,
      replyid: replyid
    });
  }
})